package com.example.timemanagementclock;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<Task> {
	Activity context = null;
	ArrayList<Task> myArray = null;
	int layoutId;

	public MyArrayAdapter(Activity context, int layoutId, ArrayList<Task> arr) {
		super(context, layoutId, arr);
		this.context = context;
		this.layoutId = layoutId;
		this.myArray = arr;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(layoutId, null);
		if (myArray.size() > 0 && position >= 0) {
			TextView txtSTT = (TextView) convertView.findViewById(R.id.txtSTT);
			TextView txtTenCongViec = (TextView) convertView
					.findViewById(R.id.txtTenCongViec);
			TextView txtGioBatDau = (TextView) convertView
					.findViewById(R.id.txtGioBatDau);
			TextView txtGioKetThuc = (TextView) convertView
					.findViewById(R.id.txtGioKetThuc);
			TextView txtNgayLamViec = (TextView) convertView
					.findViewById(R.id.txtNgayLamViec);
			ImageView ivSua = (ImageView) convertView.findViewById(R.id.ivSua);

			Task task = myArray.get(position);
			txtSTT.setText(task.getSTT() + "");
			txtTenCongViec.setText(task.getTenCongViec());

			// txtGioBatDau.setText(task.getGioBatDau());
			// SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			// txtGioBatDau.setText(sdf.format(task.getGioBatDau()));
			txtGioBatDau.setText(task.getHourFormat(task.getGioBatDau()));

			// txtGioKetThuc.setText(task.getGioKetThuc());
			// txtGioKetThuc.setText(sdf.format(task.getGioKetThuc()));
			txtGioKetThuc.setText(task.getHourFormat(task.getGioKetThuc()));

			// sdf = new SimpleDateForsmat("dd/MM/yyyy");
			// txtNgayLamViec.setText(sdf.format(task.getNgayLamViec()));
			txtNgayLamViec.setText(task.getDateFormat(task.getNgayLamViec()));

			ivSua.setImageResource(R.drawable.editicon);
			ivSua.setTag(new Integer(position));
			ivSua.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					// Toast.makeText(context,
					// "ImageView clicked for the row = "+view.getTag().toString(),
					// Toast.LENGTH_SHORT).show();
					Intent myIntent = new Intent(context, AddTaskActivity.class);
					context.startActivity(myIntent);
				}
			});
		}

		return convertView;
	}

}
